package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.crmlog.dtos.LogPageDto;
import com.huike.crmlog.pojos.CrmLog;
import com.huike.mapper.LogMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.LogService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, CrmLog> implements LogService {
    /**
     * 分页查询
     * @param logPageDto
     * @return
     */
    @Override
    public Result pageList(LogPageDto logPageDto) {
        //校验参数
        if (logPageDto==null){
            throw new RuntimeException("参数错误");
        }
        logPageDto.checkParam();
        LambdaQueryWrapper<CrmLog> logLqw=new LambdaQueryWrapper<>();
        //判断是否携带关键字模糊查询
        if (logPageDto.getKeyWords()!=null){
            logLqw.like(CrmLog::getModuleName,logPageDto.getKeyWords());
        }
        //判断是否携带时间模糊查询
        if (logPageDto.getCreateTime()!=null){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");
            LocalDateTime beginTime = LocalDateTime.of(LocalDate.parse(logPageDto.getCreateTime(), formatter), LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(LocalDate.parse(logPageDto.getCreateTime(), formatter), LocalTime.MAX);
            logLqw.between(CrmLog::getCreateTime,beginTime,endTime);
        }
        //分页查询
        Page page=new Page(logPageDto.getPage(), logPageDto.getPageSize());
        page = page(page, logLqw);
        //封装返回
        PageResult pageResult=new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getRecords());
        return Result.success(pageResult);
    }
}
